public class BinarySearch {
    public int binarySearch(int array[],int key)
    {
        int low = 0;
        int high = array.length -1;
        while(low <= high)
        {
            int mid = (low + high) / 2;
            int midVal = array[mid];

            if (midVal == key) {
                return mid;
            } else if (midVal < key) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }

}